<?php
/**
 * 生成wm6.5 manila所用的数据格式
 */
class _interface extends __interface {

	private $wcs_day_time_start = 19;	// 晚上开始时间  19:00
	private $wcs_day_time_end = 7;		// 晚上结束时间  7:00
	
	// 白天
	private $wcc_weather = array(
			'01' => '晴天',
			'02' => '以晴为主',
			'03' => '多云见晴',
			'04' => '见断多云',
			'05' => '阳光朦胧',
			'06' => '大部多云',
			'07' => '多云',
			'08' => '多云',
			'11' => '有雾',
			'12' => '阵雨',
			'13' => '大部多云，有阵雨',
			'14' => '多云间阴，有阵雨',
			'15' => '雷阵雨',
			'16' => '大部多云，有雷阵雨',
			'17' => '多云见晴，有雷阵雨',
			'18' => '有雨',
			'19' => '阵雪',
			'20' => '大部多云，有雷阵雪',
			'21' => '多云见晴，有雷阵雪',
			'22' => '雪',
			'23' => '大部多云，有雪',
			'24' => '结冰',
			'25' => '雨夹雪',
			'26' => '冻雨',
			'29' => '雨夹雪',
			'30' => '炎热',
			'31' => '寒冷',
			'32' => '有风',
			'33' => '晴',
			'34' => '大部晴朗',
			'35' => '局部多云',
			'36' => '间断多云',
			'37' => '有霾',
			'38' => '大部多云',
			'39' => '局部多云，有阵雨',
			'40' => '大部多云，有阵雨',
			'41' => '局部多云，有雷阵雨',
			'42' => '大部多云，有雷阵雨',
			'43' => '局部多云，有阵雪',
			'44' => '大部多云，有阵雪',
	);
	private $weathercn_wcc_convert = array(
			'd0' => '01',
			'd1' => '07',
			'd2' => '06',
			'd3' => '13',
			'd4' => '16',
			'd5' => '17',
			'd6' => '25',
			'd7' => '14',
			'd8' => '14',
			'd9' => '18',
			'd10' => '18',
			'd11' => '18',
			'd12' => '18',
			'd13' => '23',
			'd14' => '22',
			'd15' => '22',
			'd16' => '22',
			'd17' => '22',
			'd18' => '11',
			'd19' => '26',
			'd20' => '32',
			'd21' => '14',
			'd22' => '18',
			'd23' => '18',
			'd24' => '18',
			'd25' => '18',
			'd26' => '23',
			'd27' => '22',
			'd28' => '22',
			'd29' => '26',
			'd30' => '32',
			'd31' => '32',
			'n0' => '33',
			'n1' => '08',
			'n2' => '38',
			'n3' => '39',
			'n4' => '36',
			'n5' => '42',
			'n7' => '39',
			'n8' => '40',
			'n9' => '39',
			'n10' => '39',
			'n11' => '39',
			'n12' => '39',
			'n13' => '43',
			'n14' => '43',
			'n15' => '43',
			'n16' => '44',
			'n17' => '44',
			'n18' => '37',
			'n21' => '39',
			'n22' => '39',
			'n23' => '39',
			'n24' => '39',
			'n25' => '39',
	);
	
	private $wcc_kph = array(
			'0' => 1,
			'1' => 5,
			'2' => 11,
			'3' => 19,
			'4' => 28,
			'5' => 38,
			'6' => 49,
			'7' => 51,
			'8' => 74,
			'9' => 88,
			'10' => 102,
			'11' => 117,
			'12' => 133,
			'13' => 149,
			'14' => 166,
			'15' => 183,
			'16' => 201,
			'17' => 220
	);
	private $windDirectionMap = array(
			"南" => "S",
			"北" => "N",
			"东" => "E",
			"西" => "W",
			"东南" => "SE",
			"西南" => "WS",
			"东北" => "NE",
			"西北" => "NW",
			"微" => "None",
			"旋转" => "None",
			"无持续向" => "None"
	);
	
	/**
	 * (non-PHPdoc)
	 *
	 * @see __interface::dispose_data()
	 */
	public function dispose_data() {
		$fx = array();
		$tq1 = array();
		
		$qw1 = array();
		$qw2 = array();
		$qw3 = array();
		$cityname = $this->weather_data['city_1'];
		$province = $this->weather_data['city_2'];
		
		// 获取数据完成，开始生成指定格式数据
		$xml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>
<weather>
<location name=\"{$cityname}\" country=\"{$province}\" />
<localTime>".date("n/j/Y h:i:s A")."</localTime>";
		
		for($i = 1; $i <= $this->style ["weather_days"]; $i ++) {
			$i1 = $i * 2 - 1;
			$i2 = $i * 2;

			$date = $i>1 ? date("Y-m-d", dateadd("day", $i-1, strtotime($this->weather_data['date']))) : $this->weather_data['date'];
			
			// 处理风向数据
			$fx1[$i] = str_replace ( "风", "", $this->weather_data['windx'][$i1] );
			if(!strlen($fx1[$i])) $fx1[$i] = "微";
			$fx[$i] = $this->windDirectionMap[$fx1[$i]];

			// 处理风力数据 kph 每小时公里数 mph 每小时英里数
			$mps = str_replace ( array (
					"小于",
					"级" 
			), "", $this->weather_data ['windl'] [$i1] );
			if ($mps == "微风")
				$mps = 1;
			if (mb_strpos ( $mps, "-" ))
				$mps = mb_substr ( $mps, mb_strpos ( $mps, "-" ) + 1 );

			if (! strlen ( $mps ))
				$mps = 0;
			$wind = $fx[$i];
			$kph = isset($this->wcc_kph[$mps]) ? $this->wcc_kph[$mps] : "";
			$mph = intval ( $kph * 1.6093 );

			// 处理天气
			$tq1 [$i] = intval ( $this->weather_data ['img'] [$i1] );
			
			// 处理温度
			$qw1 [$i] = intval ( $this->weather_data ['temp'] [$i1] );
			$qw2 [$i] = intval ( $this->weather_data ['temp'] [$i2] );
			// 排除莫名其妙的BUG
			if(!isset($qw1[0])) $qw1[0] = $qw1[1];
			if(!isset($qw2[0])) $qw2[0] = $qw2[1];
			
			if ($i == 1) {
				// 处理实时温度
				if ($this->weather_data ['realtime'] ['temp'])
					$qw_now = $this->weather_data ['realtime'] ['temp'];
				else
					$qw_now = $qw1 [0] > $qw2 [0] ? $qw1 [0] : $qw2[0];
					// 如果实时温度比最高温度高，则实时温度变成最高温度，比最低温度也做此处理
				if ($qw1 [$i] > $qw2 [$i]) { // 1是高温
					if ($qw_now > $qw1 [$i])
						$qw1 [$i] = $qw_now;
					if ($qw_now < $qw2 [$i])
						$qw2 [$i] = $qw_now;
				} else { // 2是高温
					if ($qw_now > $qw2 [$i])
						$qw2 [$i] = $qw_now;
					if ($qw_now < $qw1 [$i])
						$qw1 [$i] = $qw_now;
				}
				// 处理晚上的实时天气
				$tq_now = $tq1 [$i];
				if (date ( "H" ) < $this->wcs_day_time_end || date ( "H" ) >= $this->wcs_day_time_start) {
					$tq_now = isset ( $this->weathercn_wcc_convert ['n' . $tq_now] ) ? $this->weathercn_wcc_convert ['n' . $tq_now] : $this->weathercn_wcc_convert ['d' . $tq_now];
				} else {
					$tq_now = $this->weathercn_wcc_convert ['d' . $tq_now];
				}

				$xml .= "
<current>
	<temp c=\"{$qw_now}\" f=\"" . ($qw_now * 1.8 + 32) . "\" />
	<condition icon=\"{$tq_now}\">" . $this->weathercn_wcc_convert ['d' . $tq1 [$i]] . "</condition>
</current>
<forecast>";
			}
			
			if (! $this->wcs_night_icons && (date ( "H" ) < $this->wcs_day_time_end || date ( "H" ) >= $this->wcs_day_time_start)) {
				$tq1 [$i] = isset ( $this->weathercn_wcc_convert ['n' . $tq1 [$i]] ) ? $this->weathercn_wcc_convert ['n' . $tq1 [$i]] : $this->weathercn_wcc_convert ['d' . $tq1 [$i]];
			} else {
				$tq1 [$i] = $this->weathercn_wcc_convert ['d' . $tq1 [$i]];
			}
			
			$xml .= "
<day number=\"" . ($i - 1) . "\">
	<name>" . date ( "D", strtotime ( $date ) ) . "</name>
	<date>" . date ( "n/j/Y", strtotime ( $date ) ) . "</date>
	<condition icon=\"" . $tq1[$i] . "\">" . $this->wcc_weather[$tq1[$i]] . "</condition>
	<high c=\"" . max ( $qw1 [$i], $qw2 [$i] ) . "\" f=\"" . (max ( $qw1 [$i], $qw2 [$i] ) * 1.8 + 32) . "\" />
	<low c=\"" . min ( $qw1 [$i], $qw2 [$i] ) . "\" f=\"" . (min ( $qw1 [$i], $qw2 [$i] ) * 1.8 + 32) . "\" />
	<winddir direction=\"{$wind}\"></winddir>
	<windspd kph=\"{$kph}\" mph=\"{$mph}\" mps=\"{$mps}\" />
	<uvi>0</uvi>
</day>";
		}
		$xml .= "
</forecast>
<copyright>阿枫,http://w.mdeve.com uptime:". date("Y-m-d H:i:s")."</copyright>
<usage>Manila 3D Weather www.weather.com.cn</usage>
</weather>";
		
		header("Content-Type: text/xml; charset=utf-8");
		echo $xml;
		
	}
	
	
}